﻿using MediatR;
using W.Purchase.API.Application.Command.SaleCommand;
using W.Purchase.Domain;
using W.Purchase.Repository.Interface;

namespace W.Purchase.API.Application.CommandHandler.SaleCommandHandler
{
    public class SaleOrderUpdateCommandHandler:IRequestHandler<SaleOrderUpdateCommand,int>
    {
        private readonly ISalesOrdersRepository salesOrdersRepository;

        private readonly IProductsSalesRepository productsSalesRepository;

        public SaleOrderUpdateCommandHandler(ISalesOrdersRepository salesOrdersRepository, IProductsSalesRepository productsSalesRepository = null)
        {
            this.salesOrdersRepository = salesOrdersRepository;
            this.productsSalesRepository = productsSalesRepository;
        }


        public async Task<int> Handle(SaleOrderUpdateCommand request, CancellationToken cancellationToken)
        {
            int row = 0;
            SalesOrders salesOrders = new SalesOrders();
            salesOrders.SalesOrdersId = request.SalesOrdersId;
            salesOrders.SalesOrdersNo = request.SalesOrdersNo;
            salesOrders.ProcurementTypeId = request.ProcurementTypeId;
            salesOrders.AssociateNumber = request.AssociateNumber;
            salesOrders.ClientNo = request.ClientNo;
            salesOrders.ClientName = request.ClientName;
            salesOrders.SalesOrdersPerson = request.SalesOrdersPerson;
            salesOrders.SalesOrdersPhone = request.SalesOrdersPhone;
            salesOrders.SingleMaker = request.SingleMaker;
            salesOrders.SingleTime = request.SingleTime;
            salesOrders.ShipmentsDate = request.ShipmentsDate;
            salesOrders.SalesOrdersRemark = request.SalesOrdersRemark;
            salesOrders.OrderStatus = request.OrderStatus;
            salesOrders.OrderSale = request.OrderSale;
            salesOrders.Recorded = request.Recorded;
            salesOrders.IsDelete =false ;
            row += await salesOrdersRepository.Update(salesOrders);
            List<ProductsSales> products = new List<ProductsSales>();
            foreach (var item in request.ProductsSales)
            {
                ProductsSales productsSales1 = new ProductsSales();
                productsSales1.ProductName = item.ProductName;
                productsSales1.ProductNo = item.ProductNo;
                productsSales1.ProductSpec = item.ProductSpec;
                productsSales1.UnitName = item.UnitName;
                productsSales1.Price = item.Price;
                productsSales1.ProductsSalesNum = item.ProductsSalesNum;
                productsSales1.ProductsSalesTotal = item.ProductsSalesTotal;
                productsSales1.SalesOrdersId = salesOrders.SalesOrdersId;
                productsSales1.office_id = item.office_id;
                productsSales1.user_id = item.user_id;
                products.Add(productsSales1);
            }
            row += await productsSalesRepository.UpdateRangeAsync(products);
            return row;
        }
    }
}
